<template>
  <v-layout>
    <template #header>
      <div>配送日期：
      <el-date-picker
        v-model="timeData.timeRange"
        type="datetimerange"
        range-separator="-"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        value-format="YYYY-MM-DD HH:mm:ss"
        @change = "getPurchaseList"
      />
      <el-button class="Summary" type="primary" @click="toSum">汇总</el-button>
    </div>
    </template>
    <v-table
      url="/queryPurchaseStatisticsList"
      ref="tableRef"
      :query="tableQuery"
    >
      <v-column auto type="selection"></v-column>
      <v-column auto prop="ino" label="单号"></v-column>
      <v-column auto prop="scName" label="字校名称"></v-column>
      <v-column auto prop="ghName" label="供应商"></v-column>
      <v-column auto prop="sumAmount" label="总金额"></v-column>
      <v-column auto prop="dtTime" label="创建时间" :formatter="dateFormat"></v-column>
      <v-column auto prop="status" label="状态">未配送
        <!-- <el-button size="default" >未配送</el-button> -->
      </v-column>
      
    </v-table>
  </v-layout>
</template>

<script setup lang="ts">
import { useTable } from "@/components/table/useTable";
import { markRaw, reactive, ref } from "vue";
import { useUserStore } from "@/stores/user";
import { dayjs } from "element-plus";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  sumPurchaseOrder
} from "@/api/user";
interface Data {
  scName: string;
  ghName: string;
  ghsumAmountName: number;
  dtTime: string;
  status: string;
  ino: string;
}
const { tznUser } = useUserStore();
const { tableRef, getSelectionRows, reload } = useTable();
const currentDate = dayjs().format("YYYY-MM-DD HH:mm:ss");
const time =dayjs().subtract(-1, 'day').format("YYYY-MM-DD");

const tableQuery = reactive({
  // beginTime: currentDate,
  // endTime: currentDate, //
  beginTime: time+' '+'00:00:00',
  endTime: time+' '+'23:59:59',
  vendorId: tznUser.areaId, //
  // timeRange:[time+' '+'00:00:00',time+' '+'23:59:59'], // 时间范围 [开始时间, 结束时间]
});
const timeData = reactive({
  timeRange:[time+' '+'00:00:00',time+' '+'23:59:59'], // 时间范围 [开始时间, 结束时间]
});
//时间格式化
const dateFormat =  (row) => {
    let time = new Date(row.dtTime)
    if(!time){
        return ''
    }
 
  var Y = time.getFullYear();
	var M = (time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1);
	var D = (time.getDate() < 10 ? '0' + time.getDate() : time.getDate());
	var h = (time.getHours() < 10 ? '0' + time.getHours() : time.getHours());
	var m = (time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes());
	var s = (time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds());
    return `${Y}/${M}/${D} ${h}:${m}:${s}`
}
const getPurchaseList = () =>{
  tableQuery.beginTime = timeData.timeRange[0];
  tableQuery.endTime = timeData.timeRange[1];
  tableQuery.vendorId = tznUser.areaId;
  reload();
}

const toSum = () => {  
  ElMessageBox.confirm("所选数据将进行汇总，是否确定？", "提示", {
    type: "warning",
    async callback(action: string) {
      if (action === "confirm") {
        // 发送汇总请求
        await sumPurchaseOrder(tableQuery);
        ElMessage.success("汇总信息成功");
        reload();
      }
    },
  });

  reload();
};

// 编辑
</script>
<style>
.Summary{
  float: right;
}
</style>
